﻿using System;
using Castle.Windsor;
using Microsoft.Practices.ServiceLocation;
using CommonServiceLocator.WindsorAdapter;
using Castle.MicroKernel.Registration;
using Caramel.ServiceModel.Castle;

namespace ServiceModelServiceHost
{
	public class Global : System.Web.HttpApplication
	{
		public static IWindsorContainer Container { get; set; }

		protected void Application_Start(object sender, EventArgs e)
		{
			ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(this.EnsureContainer()));
		}

		private IWindsorContainer EnsureContainer()
		{
			if (Container == null)
			{
				Container =
					new WindsorContainer().
						Register(
							Component.
								For<ITestService>().
								ImplementedBy<TestService>().
								DependsOn(Parameter.ForKey("value").Eq("From config!"))
						).
						Install(
							new ServiceModelInstaller()
						);
			}

			return Container;
		}
	}
}